import { global } from '../../global';
import { Fetch, showErrInfo } from '../../utils';

/**
 * 系统
 *
 * @author chitanda
 * @date 2022-12-13 10:12:23
 * @export
 * @class SystemService
 */
export class SystemService {
  /**
   * 获取系统列表
   *
   * @author chitanda
   * @date 2022-12-13 10:12:22
   * @return {*}  {Promise<IData[]>}
   */
  async fetch(): Promise<IData[]> {
    await this.awaitAuth();
    try {
      const res = await Fetch.get(
        this.paseUrl('/devops/systems'),
        {},
        {
          headers: this.getHeaders(),
        },
      );
      if (res.status === 200) {
        return res.data || [];
      }
    } catch (err) {
      showErrInfo(err);
    }
    return [];
  }

  /**
   * 获取系统信息
   *
   * @author chitanda
   * @date 2022-12-13 10:12:54
   * @param {string} id 系统标识
   * @return {*}  {Promise<IData | null>}
   */
  async get(id: string): Promise<IData | null> {
    await this.awaitAuth();
    try {
      const res = await Fetch.get(
        this.paseUrl(`/devops/systems/${id}`),
        {},
        {
          headers: this.getHeaders(),
        },
      );
      if (res.status === 200) {
        return res.data;
      }
    } catch (err) {
      showErrInfo(err);
    }
    return null;
  }

  /**
   * 获取指定系统插件清单
   *
   * @author chitanda
   * @date 2022-12-13 21:12:51
   * @param {string} id
   * @return {*}  {Promise<IData[]>}
   */
  async fetchPlugins(id: string): Promise<IData[]> {
    await this.awaitAuth();
    try {
      const res = await Fetch.post(
        this.paseUrl(`/devops/debug-systems/${id}/LISTPSSYSSFPLUGINS`),
        undefined,
        undefined,
        {
          headers: this.getHeaders(),
        },
      );
      if (res.status === 200) {
        return res.data || [];
      }
    } catch (err) {
      showErrInfo(err);
    }
    return [];
  }

  /**
   * 获取指定系统 debug 服务清单
   *
   * @author chitanda
   * @date 2022-12-14 15:12:56
   * @param {string} id
   * @return {*}  {Promise<IData[]>}
   */
  async fetchDebugServiceInventory(id: string): Promise<IData[]> {
    await this.awaitAuth();
    try {
      const res = await Fetch.post(
        this.paseUrl(`/devops/debug-systems/${id}/LISTSERVICEINSTANCES`),
        undefined,
        undefined,
        {
          headers: this.getHeaders(),
        },
      );
      if (res.status === 200) {
        return res.data || [];
      }
    } catch (err) {
      showErrInfo(err);
    }
    return [];
  }

  /**
   * 热更新插件代码
   *
   * @author chitanda
   * @date 2022-12-15 17:12:11
   * @param {string} system
   * @param {string} id
   * @param {string} code
   * @return {*}  {Promise<boolean>}
   */
  async hotCode(system: string, id: string, code: string): Promise<boolean> {
    try {
      const res = await Fetch.post(
        this.paseUrl(`/devops/system/${system}/applyhotcode`),
        {
          id,
          templcode: code,
        },
        undefined,
        {
          headers: this.getHeaders(),
        },
      );
      if (res.status === 200) {
        return true;
      }
    } catch (err) {
      showErrInfo(err);
    }
    return false;
  }

  /**
   * 等待认证通过
   *
   * @author chitanda
   * @date 2022-12-14 14:12:08
   * @protected
   * @return {*}  {Promise<void>}
   */
  protected async awaitAuth(): Promise<void> {
    if (global.complete) {
      return;
    }
    return global.complete_notify.wait();
  }

  protected paseUrl(url: string): string {
    return global.config.cloudAddress + url;
  }

  protected getHeaders(): Record<string, string> {
    return {
      'content-type': 'application/json',
    };
  }
}
